﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using CommonUtility.Utility;

namespace UIDevCore
{
    public class FnCode
    {
        private string _ClassName;
        public string ClassName
        {
            get { return _ClassName; }
            set { _ClassName = value; }
        }

        private string _Roles;
        public string Roles
        {
            get { return _Roles; }
            set { _Roles = value; }
        }

        private string _Module;
        public string Module
        {
            get { return _Module; }
            set { _Module = value; }
        }

        public bool HasValue
        {
            get { return !string.IsNullOrEmpty(this._Module) && !string.IsNullOrEmpty(this._ClassName); }
        }

        public FnCode(string no)
        {
            XmlDocument xml = XmlHelper.loadXml(UIConst.Fn_XmlKey,UIConst.Fn_XmlPath);
            if (xml != null)
            {
                string xpath = string.Concat(UIConst.Fn_Node, "[@no='", no, "']");
                XmlNode node = xml.SelectSingleNode(xpath);
                if (node != null)
                {
                    XmlNode nodeBR = node.ParentNode;
                    if (nodeBR != null)
                    {
                        XmlNode nodeModule = nodeBR.ParentNode;
                        if (nodeModule != null)
                        {
                            this._Roles = XmlHelper.GetNodeAttribute(node, UIConst.Fn_Roles);
                            this._ClassName = XmlHelper.GetNodeAttribute(nodeBR, UIConst.Fn_NodeName);
                            this._Module = XmlHelper.GetNodeAttribute(nodeModule, UIConst.Fn_NodeName);
                           
                            //依次向上获取角色设置
                            if (this._Roles == null)
                                this._Roles = XmlHelper.GetNodeAttribute(nodeBR, UIConst.Fn_Roles);
                            if (this._Roles == null)
                                this._Roles = XmlHelper.GetNodeAttribute(nodeModule, UIConst.Fn_Roles);
                        }
                    }
                }
            }
        }

        public UIEmnus.EError CheckRight()
        {
            UIPremission pre = new UIPremission();
            return pre.CheckRight(this._Roles);
        }
    }
}
